/**
 * @用途 窗口管理
 * @author 黄敏
 * @创建时间 2025-07-30 11:18
 **/

// @ts-ignore
import { browserTopic } from '~shared/ipc-topic'
import {
  type GetBrowserTabsParam,
  type ToBrowserMainMap,
  topicPrefix
} from '~types/message-channel'
const ipcRenderer = require('electron').ipcRenderer

/**
 * 发送消息到主进程
 * */
export function browserSend<T extends keyof ToBrowserMainMap>(topic: T) {
  return (...args: ToBrowserMainMap[T]) => {
    return ipcRenderer.send(topicPrefix + topic, ...args)
  }
}
export function browserSendSync<T extends keyof GetBrowserTabsParam>(topic: T) {
  return (...args: GetBrowserTabsParam[T]['params']) => {
    return ipcRenderer.sendSync(topicPrefix + topic, ...args) as GetBrowserTabsParam[T]['result']
  }
}

export const browser = {
  // 窗口管理方法
  create: (tabIds?: number[]) => ipcRenderer.send(browserTopic.addBrowser, tabIds),
  moveTabsToCurrentBrowser: (tabIds: number[]) => ipcRenderer.send(browserTopic.moveTabs, tabIds),

  addTab: browserSend('addTab'),
  closeTabs: browserSend('closeTabs'),
  hideTab: browserSend('hideTab'),
  setActiveTab: browserSend('setActiveTab'),
  setLayer: browserSend('setLayer'),
  minimizeWindow: browserSend('minimizeWindow'),
  maximizeWindow: browserSend('maximizeWindow'),
  unmaximizeWindow: browserSend('unmaximizeWindow'),
  createGroup: browserSend('createGroup'),
  closeWindow: browserSend('closeWindow'),
  getTopTabs: browserSendSync('getBrowserTopTabs'),
  getLeftTabs: browserSendSync('getBrowserLeftTabs'),
  getRightTabs: browserSendSync('getBrowserRightTabs'),
  getActiveTabId: browserSendSync('getActiveTabId'),
  isMainWindow: browserSendSync('isMainWindow'),
  getSeparateOpenIds: browserSendSync('getSeparateOpenIds'),
  isWindowMaximized: browserSendSync('isWindowMaximized')
}
